package com.hjm.project.walletDetails.walletDetails.service.impl;

import java.util.List;
import java.util.Map;

import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.StringUtils;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.project.walletDetails.walletDetails.domain.PaymentExport;
import com.hjm.project.walletDetails.walletDetails.domain.WalletDetails;
import com.hjm.project.walletDetails.walletDetails.utils.SendUtils;
import com.yxhd.encrypt.EncryptUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hjm.project.walletDetails.walletDetails.mapper.WalletDetailsMapper;
import com.hjm.project.walletDetails.walletDetails.service.IWalletDetailsService;
import com.hjm.common.utils.text.Convert;

/**
 * 服务商钱包明细Service业务层处理
 * 
 * @author WalletDetails
 * @date 2020-11-25
 */
@Service
public class WalletDetailsServiceImpl implements IWalletDetailsService
{
    @Autowired
    private WalletDetailsMapper walletDetailsMapper;

    /**
     * 查询服务商钱包明细
     * 
     * @param id 服务商钱包明细ID
     * @return 服务商钱包明细
     */
    @Override
    public WalletDetails selectwalletDetailsById(Long id)
    {
        return walletDetailsMapper.selectwalletDetailsById(id);
    }

    /**
     * 查询服务商钱包明细列表
     * 
     * @param walletDetails 服务商钱包明细
     * @return 服务商钱包明细
     */
    @Override
    public List<WalletDetails> selectwalletDetailsList(WalletDetails walletDetails)
    {
        return walletDetailsMapper.selectwalletDetailsList(walletDetails);
    }

    @Override
    public List<Map> agentAccountList(Map params) {
        return walletDetailsMapper.agentAccountList(params);
    }
    @Override
    public Map agentAccountListSum(Map params) {
        return walletDetailsMapper.agentAccountListSum(params);
    }

    /**
     * 新增服务商钱包明细
     * 
     * @param walletDetails 服务商钱包明细
     * @return 结果
     */
    @Override
    public int insertwalletDetails(WalletDetails walletDetails)
    {
        walletDetails.setCreateTime(DateUtils.getNowDate());
        return walletDetailsMapper.insertwalletDetails(walletDetails);
    }

    @Override
    public int saveAccountUpdate(Map map) {

        return walletDetailsMapper.saveAccountUpdate(map);
    }

    @Override
    public AjaxResult saveAccountAdjust(Map map) {
        AjaxResult ajaxResult = SendUtils.adjustAgentMoney(
                map.get("agentNum").toString(),
                map.get("money").toString(),
                map.get("adjustType").toString(),
                map.get("accountType").toString());
        return ajaxResult;
    }

    /**
     * 修改服务商钱包明细
     * 
     * @param walletDetails 服务商钱包明细
     * @return 结果
     */
    @Override
    public int updatewalletDetails(WalletDetails walletDetails)
    {
        walletDetails.setUpdateTime(DateUtils.getNowDate());
        return walletDetailsMapper.updatewalletDetails(walletDetails);
    }
    public int updateAgentAccount(Map params)
    {
        return walletDetailsMapper.updateAgentAccount(params);
    }

    /**
     * 删除服务商钱包明细对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletewalletDetailsByIds(String ids)
    {
        return walletDetailsMapper.deletewalletDetailsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除服务商钱包明细信息
     * 
     * @param id 服务商钱包明细ID
     * @return 结果
     */
    @Override
    public int deletewalletDetailsById(Long id)
    {
        return walletDetailsMapper.deletewalletDetailsById(id);
    }

    @Override
    public List<WalletDetails> selectPaymentList(WalletDetails walletDetails) {
        return walletDetailsMapper.selectPaymentList(walletDetails);
    }

    @Override
    public List<PaymentExport> exportPaymentList(WalletDetails walletDetails) throws Exception {

        //查询满足条件的Id
        List<Long> idList = walletDetailsMapper.exportPaymentIdList(walletDetails);
        walletDetails.setList(idList);
        List<PaymentExport> list = walletDetailsMapper.exportPaymentList(walletDetails);
        for (PaymentExport paymentExport : list) {
            if (StringUtils.isNotEmpty(paymentExport.getScreenNum())) {
                paymentExport.setScreenNum(EncryptUtils.getDecrypt(paymentExport.getScreenNum()));
            }
        }
        if (CollectionUtils.isNotEmpty(list)) {
            //修改代付数据状态
            walletDetailsMapper.updatePaymentStatus(idList);
        }
        return list;
    }

    @Override
    public int markingFailed(String ids) {
        return walletDetailsMapper.markingFailed(Convert.toStrArray(ids));
    }
}
